<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>标准规范</title>
    <meta name="renderer" content="webkit">
    <#--   <link rel="stylesheet" href="${re.contextPath}/plugin/lay/layui/css/layui.css">-->
    <!--layui样式-->
    <link rel="stylesheet" href="${re.contextPath}/plugin/layui/css/layui.css" media="all"/>
    <!--layui-dropdown-->
    <link rel="stylesheet" type="text/css" href="${re.contextPath}/plugin/layui/dropdown_2.3.3/dropdown.css">
    <!--公用样式-->
    <link rel="stylesheet" type="text/css" href="${re.contextPath}/plugin/common/css/comment.css"/>
    <!--页面样式-->
    <link rel="stylesheet" type="text/css" href="${re.contextPath}/plugin/common/css/style.css"/>
    <!--扩展样式-->
    <link rel="stylesheet" href="${re.contextPath}/plugin/common/css/extended.css">
    <script type="text/javascript" src="${re.contextPath}/plugin/jquery/jquery-3.2.1.min.js"></script>
    <script type="text/javascript" src="${re.contextPath}/plugin/layui/layui.js" charset="utf-8"></script>
    <!-- 公共配置第三方组件 -->
    <script type="text/javascript">
        layui.config({
            version: false,
            debug: false,
            base: "${re.contextPath}/plugin/layui/dropdown_2.3.3/"
        })
    </script>
    <style>
        .select ul li:nth-child(4)>.ml10,
        .select ul li:nth-child(5)>.ml10{
            margin-left: 5px;
        }
        .select ul li:nth-child(4) .money{
            width: 90%;
        }
        .select ul li:nth-child(5) .money{
            width: 71%;
        }
        .select ul li:nth-child(4)>div{
            width: 77%;
        }
        .select ul li:nth-child(4) .money>div{
            width: 48%;
        }
        .select ul li:nth-child(5) .money>div{
            width: 43%;
        }
        .select ul li:nth-child(4) .money>div input{
            width: 95%;
        }
        .select ul li:nth-child(5) .money>div input{
            width: 60%;
        }
        .layu-menu-item-wrap{
            float:none!important;
            width: 100% !important;
        }
        .screening ul li input {
            width: 65%;
            padding-left: 5%;
            border: 0;
        }

        .screening ul li>select {
            border: 1px solid #DDDDDD;
        }

        .screening .money div {
            border: 1px solid #DDDDDD;
        }
    </style>
</head>
<body>
<div class="screening whitebg clearfix">
    <div class="select">
        <ul class="clearfix">
            <li>
                <span>文档名称</span>

                <input type="text" style="border:1px solid #DDDDDD;" name="fileName" id="fileName" placeholder="请输入文档名称">
            </li>

            <li>
                <span>归属标段</span>
                <select id="pcId">
                    <option value="">请选择</option>
                    <#list listfb as vo>
                        <option value="${vo.id}">${vo.code}</option>
                    </#list>
                </select>
            </li>
        </ul>
        <div class=" project" style="margin-top: 0px;width: 100%;padding-left: 0px;">
            <div class="fl clearfix">
                    <a href="#" class="fr white ft14 btn_del" onclick="exportall()">批量导出</a>
                    <#if '${isxmjl}'=='1'>
                      <a href="#" class="fr white ft14 " onclick="deleteFile()">批量删除</a>
                    </#if>

            </div>
            <div class="fr clearfix">
                <a href="#" class="fr white ft14 btn_my" onclick="query()">筛选</a>
                <a href="#" class="fr white ft14 btn_del" onclick="reset()">重置</a>
            </div>
        </div>
        <div class="project whitebg" style="width: 100%;padding-left: 0px;margin-top: 0px;padding-top: 10px;margin-right: 0px;padding-right: 0px;">
            <table id="projectList" class="layui-hide" lay-filter="project"></table>
        </div>
    </div>
</div>

<script type="text/javascript" src="${re.contextPath}/plugin/tools/tool.js"></script>

<script>
    var type = "${type}";
    var projectId = "${projectId}";
    var isxmjl = "${isxmjl}";

    var layer;
    var form = layui.form;
    var table = null;
    var tableIns = null;
    document.onkeydown = function (e) { // 回车提交表单
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which;
        if (code == 13) {
            $(".select .select-on").click();
        }
    }
    //注意进度条依赖 element 模块，否则无法进行正常渲染和功能性操作
    var element,dropdown;
    layui.use(['element','dropdown'], function () {
        element = layui.element , dropdown = layui.dropdown;
    });

    layui.use('upload', function(){
        var upload = layui.upload;
        //执行实例
        var uploadInst = upload.render({
            elem: '#test1' //绑定元素
            ,url: '/project/projectManager/jlbzgf/addFile' //上传接口
            ,accept: 'file'
            ,multiple: true,
            number:2
            ,drag: true
            ,allDone: function(obj){ //当文件全部被提交后，才触发
                console.log(obj.total); //得到总文件数
                console.log(obj.successful); //请求成功的文件数
                console.log(obj.aborted); //请求失败的文件数
                layer.msg("上传成功", { icon: 1, time: 3000 })
                query();
                // 刷新列表
            }
            ,done: function(res){
                //上传完毕回调
                console.log(res);
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });

    var tableCols =  [[
        {checkbox: true, fixed: true, width: '5%'},
        {field: 'fileName', title: '文档名称', width: '30%', sort: false,align: 'center'},
        {field: 'fbCode', title: '归属标段', width: '30%', sort: false,align: 'center'},
        {field: 'realName', title: '上传人', width: '20%', sort: false,align: 'center'},
        {field: 'createDateStr', title: '上传时间',align: 'center'}
     ]];
    layui.use('table', function () {
        table = layui.table;
        element = element;
        //方法级渲染
        tableIns = table.render({
            id: 'projectList',
            elem: '#projectList',
            autoSort: false,
            height: "auto",
            url: '/project/projectManager/showProjectFileList?projectId='+projectId,
            cols:tableCols,
            page: true,
            done: function (res, currentCount) {
                console.log("done......")
                element.render();
                $(".layui-table-page select").css("width","80px");
                var  progressbar =$(".layui-table-main").find(".layui-progress-bar");
                $(progressbar).each(function (i, v) {
                    var percent = $(v).attr("lay-percent");
                    if(percent){
                       var per= percent.replace("%","");
                        if(parseFloat(per)<5  && parseFloat(per)>0){
                            $(v).css("width","5%");
                        }
                    }
                });
                dropdown.suite();
                //如果页面缓存中有需要跳转的页面,跳转页面,用户监理系统外打开项目查看页面
                // openProjInfo();
            }
        });
        // 监听排序 解决 项目进度排序后不显示的问题
        table.on('sort(project)', function (obj) {
            //遍历设置目前的宽度：
            saveTableWidth();
            table.reload('projectList', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field,//排序字段
                    order: obj.type//排序方式
                },
                cols: tableCols
            });
        });
    });

    layui.use('laydate', function () {
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#totalDurationBeginDate' //指定元素
        });
        laydate.render({
            elem: '#totalDurationEndDate' //指定元素
        });
    });
    function openUpload() {

        var title = "文件上传";
        var w = "";
        var h = "";
        var url = "/project/projectManager/jlbzgf/showUpload?type="+type;
        if (title == null || title == '') {
            title = false;
        }
        ;
        if (url == null || url == '') {
            url = "404.html";
        }
        ;
        if (w == null || w == '') {
            w = 800;
        }
        ;
        if (h == null || h == '') {
            h =700;
        }
        ;
        layer.open({
            id: 'open-upload',
            type: 2,
            area: [w + 'px', h + 'px'],
            fix: false,
            maxmin: true,
            shadeClose: false,
            shade: 0.4,
            title: title,
            content:url,
            // 关闭时刷新列表
            end: function (index, layero) {
                layer.close(index);
                console.log("ppppppppppppppppppppppppp")
                query()

            }

        });
    }

    function reset(){
        $('#fileName').val("");
        $('#pcId').val("");
        var jsondata = {
            fileName: "",
            pcId:""
        };
        table.reload('projectList', {where: jsondata,cols: tableCols});
    }


    function query() {
        var fileName = $('#fileName').val();
        var pcId = $('#pcId').val();
        var jsondata = {
            fileName: fileName,
            pcId:pcId
        };
        table.reload('projectList', {where: jsondata,cols: tableCols});
    }


    /**
     * 删除
     * */
    function deleteFile(id) {
        var checkStatus = table.checkStatus('projectList');
        var data = checkStatus.data;
        //获取选中数量
        var selectCount = checkStatus.data.length;
        if (data.length == 0) {
            layer.msg('请选择文件进行删除。', {icon: 5});
            return false;
        } else {
            var proNames = [];
            for (var i = 0; i < selectCount; i++) {
                proNames.push(checkStatus.data[i].projectName);
            }
            layer.confirm('确定将 '+proNames+' 文件删除？', function (index) {
                var ids = [];
                for (var i = 0; i < selectCount; i++) {
                    ids.push(checkStatus.data[i].id);
                }
                var ids = ids.join(',');
                layerAjax("/project/projectManager/project/deleteFile", {ids: ids}, "", function (d) {
                    if (d.flag) {
                        layer.msg("操作成功", {icon: 1});
                        tableIns.reload();
                    } else {
                        layer.msg("操作失败", {icon: 2});
                    }
                })
            });
        }
    }


    function exportall(){

        var checkStatus = table.checkStatus('projectList');
        var data = checkStatus.data;
        //获取选中数量
        var selectCount = checkStatus.data.length;

        if (data.length == 0) {
            layer.msg('请选择文件进行导出。', {icon: 5});
            return false;
        }

        var ids = [];
        for (var i = 0; i < selectCount; i++) {
            ids.push(checkStatus.data[i].id);
        }
        if(selectCount>10){
            layer.msg('最多只能选择10个文件进行导出。', {icon: 5});
            return false;
        }
        console.log(ids,"选择的文件id");
        parent.layer.confirm('确定要导出所选择的文档？', function (index) {
            parent.layer.close(index);
            // 显示加载提示
            var loadingIndex = layer.load(1);
            var completedCount = 0;
            var successCount = 0;
            for (var i = 0; i < ids.length; i++) {
                (function(id) {
                    $.ajax({
                        url: '/project/projectManager/projectFile/export',
                        type: 'GET',
                        data: { id: id },
                        xhrFields: {
                            responseType: 'blob'
                        },
                        success: function(blob, textStatus, xhr) {
                            // 从响应头中获取文件名
                            var filename = '';
                            var contentDisposition = xhr.getResponseHeader('Content-Disposition');
                            const userAgent = navigator.userAgent;
                             filename =getFilenameFromContentDisposition(contentDisposition,userAgent);
                            var url = window.URL.createObjectURL(blob);
                            var a = document.createElement('a');
                            a.href = url;
                            a.download = filename;
                            document.body.appendChild(a);
                            a.click();
                            a.remove();
                            window.URL.revokeObjectURL(url);
                            successCount++;
                        },
                        error: function(xhr, status, error) {
                            layer.msg('导出失败：' + error, {icon: 5});
                            // 记录已完成的导出数量
                            completedCount++;
                            if (completedCount === ids.length) {
                                // 所有文件导出完成，关闭加载提示
                                layer.close(loadingIndex);
                            }
                        },
                        complete: function() {
                            // 记录已完成的导出数量
                            completedCount++;
                            if (completedCount === ids.length) {
                                // 所有文件导出完成，关闭加载提示
                                layer.close(loadingIndex);

                                if (successCount === ids.length) {
                                    // 所有文件都成功导出
                                    layer.msg('所有文件已成功导出。', {icon: 6});
                                } else {
                                    // 有部分文件导出失败
                                    layer.msg('部分文件导出失败，请检查。', {icon: 5});
                                }
                            }
                        }
                    });
                })(ids[i]);
            }

        });
    }



    function saveTableWidth(){
        //把表格的宽度设置到数组里面：
        var theTable = $(".layui-table")[0];
        var theThs = $(theTable).find('th');
        $.each(theThs,function(ind,e){
            if(ind > 6){
                return false;
            }
            tableCols[0][ind].width = $(e)[0].clientWidth;
        })
    }
    function getFilenameFromContentDisposition(contentDisposition, userAgent) {
        const regex = /filename=(.*?)(;|$)/;
        if (contentDisposition) {
            const match = contentDisposition.match(regex);
            if (match && match[1]) {
                let filename = match[1].trim();
                if (userAgent.includes('MSIE') || userAgent.includes('Trident')) {
                    // IE 浏览器
                    filename = decodeURIComponent(filename);
                } else {
                    // 其他浏览器
                    filename = decodeURIComponent(escape(filename));
                }
                return filename;
            }
        }
        return 'file_' + id + '.xlsx'; // 默认文件名
    }


</script>

<script type="text/html" id="shbz">
    {{#  if(d.shbz == '0'){ }}
    停用
    {{#  } else if(d.shbz == '1'){ }}
    正常
    {{#  } else{ }}
    '
    <div class="layui-progress layui-progress-big" lay-showpercent="true">
        <div class="layui-progress-bar layui-bg-orange" lay-percent="222%">111111</div>
    </div><br>'

    {{#  }  }}

</script>


<script type="text/html" id="zt">
    {{#  if(d.status == '000'){ }}
    已删除
    {{#  } else if(d.status == '001'){ }}
    未提交
    {{#  } else if(d.status == '002'){ }}
    未启动
    {{#  } else if(d.status == '004'){ }}
    进行中
    {{#  } else if(d.status == '003'){ }}
    已完成
    {{#  } else if(d.status == '005'){ }}
    未已延期
    {{#  } }}
</script>

</body>

</html>
